package com.b1.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.FileUtils;
import com.b1.constant.CommonConstant;
import com.b1.mapper.PartolSleepRecordMapper;

import com.b1.pojo.dto.partolSleepRecord.PartolSleepExportRecord;
import com.b1.pojo.dto.partolSleepRecord.PartolSleepRecordQueryRequest;
import com.b1.pojo.enity.PartolSleepRecord;
import com.b1.pojo.enums.*;
import com.b1.service.PartolSleepRecordService;
import com.b1.utils.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.b1.utils.AliOssUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author ThinkBook
* @description 针对表【partol_sleep_record(巡查人午睡管理记录表)】的数据库操作Service实现
* @createDate 2024-10-09 15:46:21
*/
@Service
public class PartolSleepRecordServiceImpl extends ServiceImpl<PartolSleepRecordMapper, PartolSleepRecord>
    implements PartolSleepRecordService{

    @Override
    public QueryWrapper<PartolSleepRecord> getQueryWrapper(PartolSleepRecordQueryRequest partolSleepRecordQueryRequest) {
        QueryWrapper<PartolSleepRecord> queryWrapper = new QueryWrapper<>();
        String className = partolSleepRecordQueryRequest.getClassName();
        Integer bedNum = partolSleepRecordQueryRequest.getBedNum();
        Integer actualNum = partolSleepRecordQueryRequest.getActualNum();
        String teacherDesc = partolSleepRecordQueryRequest.getTeacherDesc();
        Double sleepRate = partolSleepRecordQueryRequest.getSleepRate();
        String childDesc = partolSleepRecordQueryRequest.getChildDesc();
        Integer isError = partolSleepRecordQueryRequest.getIsError();
        Date patrolTimeStart = partolSleepRecordQueryRequest.getPatrolTimeStart();
        Date patrolTimeEnd = partolSleepRecordQueryRequest.getPatrolTimeEnd();
        String patrolName = partolSleepRecordQueryRequest.getPatrolName();
        String kindomName = partolSleepRecordQueryRequest.getKindomName();
        String sortField = partolSleepRecordQueryRequest.getSortField();
        String sortOrder = partolSleepRecordQueryRequest.getSortOrder();

        //根据条件构造查询条件
        //模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(teacherDesc), "teacherDesc", teacherDesc);
        queryWrapper.like(StringUtils.isNotEmpty(childDesc), "childDesc", childDesc);
        //精确查询
        queryWrapper.eq(StringUtils.isNotEmpty(className), "className", className);
        queryWrapper.eq(ObjectUtils.isNotEmpty(bedNum), "bedNum", bedNum);
        queryWrapper.eq(ObjectUtils.isNotEmpty(actualNum), "actualNum", actualNum);
        queryWrapper.eq(ObjectUtils.isNotEmpty(sleepRate), "sleepRate", sleepRate);
        queryWrapper.eq(ObjectUtils.isNotEmpty(isError), "isError", isError);
        queryWrapper.eq(StringUtils.isNotEmpty(kindomName), "kindomName", kindomName);
        queryWrapper.eq(StringUtils.isNotEmpty(patrolName), "patrolName", patrolName);
        queryWrapper.between(ObjectUtils.isNotEmpty(patrolTimeEnd)&&ObjectUtils.isNotEmpty(patrolTimeStart), "patrolTime", patrolTimeStart, patrolTimeEnd);

        //分页查询
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
    @Autowired
    private AliOssUtil aliOssUtil;
    @Override
    public String export(Long[] ids) throws IOException {
        List<PartolSleepRecord> partolSleepRecord = listByIds(Arrays.asList(ids));
        List<PartolSleepExportRecord> partolSleepExportRecord = new ArrayList<>();
        for (PartolSleepRecord record : partolSleepRecord) {
            PartolSleepExportRecord export = new PartolSleepExportRecord();
            BeanUtil.copyProperties(record,export);
            export.setIsError(YesOrNoEnum.getEnumByValue(record.getIsError()).getText());
            partolSleepExportRecord.add(export);
        }
        String upload;
        //创建一个临时文件写入
        File file = new File("partolSleepExportRecord.xlsx");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            //将数据写入临时文件
            EasyExcel.write(fos, PartolSleepExportRecord.class).sheet("巡查人午睡管理记录表").doWrite(partolSleepExportRecord);
        }catch (Exception exception){
            throw new RuntimeException("导出失败");
        }
        //将临时文件上传到阿里云oss
        String fileName="巡查人午睡管理记录表"+System.currentTimeMillis()+".xlsx";
        //将 file转为byte
        byte[] bytes = FileUtils.readFileToByteArray(file);
        upload = aliOssUtil.upload(bytes, fileName);
        return upload;
    }
}




